
package study.tag;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.Assert;

import com.barney.support.web.WebSearch;

/**
 * URL 태그
 *
 * @author Barney Kim
 */
public class UrlTag {
	
	public static final String VERSION;
	
	public static final String RESOURCE_PREFIX = "/resources";
	
	static {
		VERSION = populateVersion();
	}
	
	private static String populateVersion() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
		return sdf.format(new Date());
	}
	
	public static String jsUrl(String url) {
		return resourceUrl(url);
	}
	
	public static String jsLibUrl(String url) {
		return resourceUrl(url);
	}
	
	public static String cssUrl(String url) {
		return resourceUrl(url);
	}
	
	public static String imgUrl(String url) {
		return resourceUrl(url);
	}
	
	/**
	 * resource URL을 반환한다.
	 *
	 * @param url
	 */
	public static String resourceUrl(String url) {
		Assert.hasText(url, "url을 지정해야 합니다.");
		return String.format("%s%s?v=%s", RESOURCE_PREFIX, url, VERSION);
	}
	
	public static String urlForSearch(WebSearch search) {
		return search.toUrl();
	}
	
	public static String urlWithSearch(String prefixURI, WebSearch search) {
		return search.toUrl(prefixURI);
	}
}
